import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
#color = sns.color_palette()
#input data
train = pd.read_csv("./pima-indians-diabetes.csv")
train.head()
#查看数值型特征的基本统计量
train.describe()
NaN_col_names = ['Plasma_glucose_concentration','blood_pressure','Triceps_skin_fold_thickness','serum_insulin','BMI']
train[NaN_col_names] = train[NaN_col_names].replace(0, np.NaN)
print(train.isnull().sum())
print((train[NaN_col_names] == 0).sum())
#缺失值比较多，干脆就开一个新的字段，表明是缺失值还是不是缺失值
train['Triceps_skin_fold_thickness_Missing'] = train['Triceps_skin_fold_thickness'].apply(lambda x: 1 if pd.isnull(x) else 0)
train[['Triceps_skin_fold_thickness','Triceps_skin_fold_thickness_Missing']].head(10)


#%matplotlib inline
sns.countplot(x="Triceps_skin_fold_thickness_Missing", hue="Target",data=train)
plt.show()
#缺失值比较多，干脆就开一个新的字段，表明是缺失值还是不是缺失值
train['serum_insulin_Missing'] = train['serum_insulin'].apply(lambda x: 1 if pd.isnull(x) else 0)
sns.countplot(x="serum_insulin_Missing", hue="Target",data=train)
plt.show()
#不过特征是否缺失好像和目标也没什么关系
train.drop(["Triceps_skin_fold_thickness_Missing", "serum_insulin_Missing"], axis=1, inplace=True)
#感觉特征缺失是随机的，将这新增的特征删除，老实用中值填补
medians = train.median()
train = train.fillna(medians)

print(train.isnull().sum())

#   数据标准化

#  get labels
y_train = train['Target']
X_train = train.drop(["Target"], axis=1)
#用于保存特征工程之后的结果
feat_names = X_train.columns
# 数据标准化
from sklearn.preprocessing import StandardScaler
# 初始化特征的标准化器
ss_X = StandardScaler()
# 分别对训练和测试数据的特征进行标准化处理
X_train = ss_X.fit_transform(X_train)
#特征处理结果存为文件

#存为csv格式
X_train = pd.DataFrame(columns = feat_names, data = X_train)
train = pd.concat([X_train, y_train], axis = 1)
train.to_csv('FE_pima-indians-diabetes.csv', index=False, header=True)
